local assets =
{
    Asset("ANIM", "anim/stealingknife.zip"),  --地上的动画
    Asset("ANIM", "anim/swap_stealingknife.zip"), --手里的动画
	Asset("ATLAS", "images/stealingknife.xml"), --加载物品栏贴图
    Asset("IMAGE", "images/stealingknife.tex"),
}

local function CheckSpawnedLoot(loot)
    if loot.components.inventoryitem ~= nil then
        loot.components.inventoryitem:TryToSink()
    else
        local lootx, looty, lootz = loot.Transform:GetWorldPosition()
        if ShouldEntitySink(loot, true) or TheWorld.Map:IsPointNearHole(Vector3(lootx, 0, lootz)) then
            SinkEntity(loot)
        end
    end
end

local function DropLoot(inst, loot)
   
    local x, y, z = inst.Transform:GetWorldPosition()
    if loot.Physics ~= nil then
        local angle = math.random() * 2 * PI
        loot.Physics:SetVel(2 * math.cos(angle), 10, 2 * math.sin(angle))

        if inst.Physics ~= nil then
            local len = loot:GetPhysicsRadius(0) + inst:GetPhysicsRadius(0)
            x = x + math.cos(angle) * len
            z = z + math.sin(angle) * len
        end

        loot:DoTaskInTime(1, CheckSpawnedLoot)
    end
    loot.Transform:SetPosition(x, y, z)
    --loot:PushEvent("on_loot_dropped", {dropper = inst})

    return loot
end

local function OnHitohter(inst, data) 
    local weapon = data.weapon
    local target = data.target
    if weapon == nil or weapon.prefab ~= "stealingknife" then
        return
    end

    local target_loot
    if target ~= nil and target.components.lootdropper ~= nil then
        target_loot = target.components.lootdropper:OldGenerateLoot()
    end

    local start = target ~= nil and target.components.health ~= nil and (target.components.health.maxhealth*0.001) or 1
    start = math.floor(start)

    local loot_data = {"meat", "goldnugget", "blueprint", "redgem", "greengem", "purplegem", "bluegem", "orangegem", "yellowgem", "skillbookpage", "diamond"}
    local loot_prefab = "goldnugget"
    local chance = math.random()
    if chance < 0.33 then
        if target_loot ~= nil and #target_loot > 0 then
            loot_prefab = target_loot[math.random(#target_loot)]
        end
    elseif chance < 0.66 then
        start = math.min(start, #loot_data-1)
        loot_prefab = loot_data[math.random(start, #loot_data)]
    end
    local loot = SpawnPrefab(loot_prefab)
    DropLoot(target or inst, loot)
end

----------------------
local function onequip(inst, owner) --装备时
    owner.AnimState:OverrideSymbol("swap_object", "swap_stealingknife", "material")
								--替换的动画部件	使用的动画.zip	替换的贴图所在文件夹（注意这里也是素材图片文件夹的名字）
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

    inst:ListenForEvent("onhitother", OnHitohter, owner)
end
local function onunequip(inst, owner) --解除时
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    inst:RemoveEventCallback("onhitother", OnHitohter, owner)
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("stealingknife")
    inst.AnimState:SetBuild("stealingknife")
    inst.AnimState:PlayAnimation("idle")

    --inst:AddTag("sharp")
    --inst:AddTag("pointy")
    inst:AddTag("fixedless")

    inst:AddTag("weapon")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    -- 武器组件
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(35) --设置伤害

    -- 耐久组件
    inst:AddComponent("finiteuses") 
    inst.components.finiteuses:SetMaxUses(30)
    inst.components.finiteuses:SetUses(30)

    inst.components.finiteuses:SetOnFinished(inst.Remove) --没有耐久了移除武器

    inst:AddComponent("inspectable") --可检查组件

    inst:AddComponent("inventoryitem") --物品组件
	inst.components.inventoryitem.atlasname = "images/stealingknife.xml" --物品贴图
	
    inst:AddComponent("equippable") --可装备组件
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    MakeHauntableLaunch(inst)

    return inst
end

return Prefab("stealingknife", fn, assets)